import { download } from "@/http/request"
import axios from "axios"
import { ElMessage } from 'element-plus'
// 下载文件
export const downloadFile = (url, params, fileName) => {
  return download(url, params, fileName)
}

export async function downLoadBlobFile(url, fileName) {
  const request = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_URL,
    responseType: 'blob',
  });
  try {
    const response = await request.get(url);
    const url_4 = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url_4;
    // 提取文件名
    let contentDispositionHeader = response.headers['content-disposition'].split('filename="')[1].split('"')[0]
    let name = fileName;
    if (contentDispositionHeader) {
      name = decodeURIComponent(contentDispositionHeader);
    }
    // 设置下载文件的属性（名称和类型）
    link.setAttribute('download', name);
    document.body.appendChild(link);
    link.click();
    // 清理创建的临时链接
    URL.revokeObjectURL(url_4);
    ElMessage({
      message: '下载成功',
      type: 'success',
    })
  } catch (error) {
    ElMessage({
      message: '文件不存在',
      type: 'error',
    })
    console.error(error);
  }
}